package studio.raptor.extractor.segment.task.assemble;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.extractor.common.util.Pair;
import studio.raptor.extractor.segment.constant.EventKindConstant;
import studio.raptor.extractor.segment.constant.JsonElementName;
import studio.raptor.extractor.segment.entity.EventMeta;
import studio.raptor.extractor.segment.task.BuildJsonTask;
import studio.raptor.hub.in.entity.ServerProxyReq;
import studio.raptor.hub.in.server.*;

/**
 * Created by liuyong8 on 2017/7/27.
 */
class NodeMsgTask implements Callable<Pair<String, String>> {
  private static final Logger log = LoggerFactory.getLogger(NodeMsgTask.class);
  private EventMeta eventMeta;
  private String jsonNodeName;
  private String serverUrl;
  private HttpClient httpClient;
  private String serverType;

  public NodeMsgTask(EventMeta eventMeta, String jsonNodeName, String serverUrl,HttpClient httpClient, String serverType) {
    this.eventMeta = eventMeta;
    this.jsonNodeName = jsonNodeName;
    this.serverUrl = serverUrl;
    this.httpClient = httpClient;
    this.serverType = serverType;
  }

  @Override
  public Pair<String, String> call() {
    StringBuilder sb = new StringBuilder();
    Integer kindId = eventMeta.getEventKindId();
    List<Long> params = new ArrayList<>();
    ServerProxyReq serverProxyReq = new ServerProxyReq();
    serverProxyReq.setUrl(serverUrl);
    serverProxyReq.setServerType(serverType);


    try {
      if (JsonElementName.CUSTOMER_ORDER.equals(jsonNodeName) || JsonElementName.CUSTOMER_ORDERS.equals(jsonNodeName)) {
        if (EventKindConstant.SO_COMMIT_ORDER.equals(kindId)) {
          SoCommitOrderProxy soCommitOrderProxy = new SoCommitOrderProxy.Builder().httpClient(httpClient).build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(soCommitOrderProxy.queryServerMsg(serverProxyReq)).append(",");
        } else if (EventKindConstant.SO_EXECUTION.equals(kindId)) {
          SoExecutionProxy soExecutionProxy = new SoExecutionProxy.Builder().httpClient(httpClient).build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(soExecutionProxy.queryServerMsg(serverProxyReq)).append(",");
        } else if (EventKindConstant.SO_COMPLETE_ORDERITEM.equals(kindId)) {
          // 由蒿亭那边处理，不走databus
        } else if (EventKindConstant.SO_COMPLETE_ARCHIVEGROUP.equals(kindId)) {
          SoCompleteARPProxy soCompleteARPProxy = new SoCompleteARPProxy.Builder().httpClient(httpClient).build();
          String content = eventMeta.getContent();
          List<Long> orderItemIds = JsonPath.parse(content).read("$.orderItemIds");
          params.addAll(orderItemIds);
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(soCompleteARPProxy.queryServerMsg(serverProxyReq)).append(",");
        } else if (EventKindConstant.SO_COMPLETE_ORDER.equals(kindId)) {
          SoCommitOrderProxy soCommitOrderProxy = new SoCommitOrderProxy.Builder().httpClient(httpClient).build();
          params.add(Long.valueOf(eventMeta.getObjectKey()));
          serverProxyReq.setParams(params.toArray(new Long[] {}));
          sb.append(soCommitOrderProxy.queryServerMsg(serverProxyReq)).append(",");
        }
      } else if (JsonElementName.CUSTOMER_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getCustId() != null){
          for (Long custId : eventMeta.getCustId()) {
            CustomerProxy customerProxy = new CustomerProxy.Builder().httpClient(httpClient).build();
            params.add(custId);
            serverProxyReq.setParams(params.toArray(new Long[] {}));
            sb.append(customerProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        }else {
          sb.append(",");
        }
      } else if (JsonElementName.ACCOUNT_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getAcctId() != null){
          for (Long acctId : eventMeta.getAcctId()) {
            AccountProxy accountProxy = new AccountProxy.Builder().httpClient(httpClient).build();
            params.add(acctId);
            serverProxyReq.setParams(params.toArray(new Long[] {}));
            sb.append(accountProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        }else {
          sb.append(",");
        }
      } else if (JsonElementName.OFFER_INST_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getOfferInstId() != null){
          for (Long offerId : eventMeta.getOfferInstId()) {
            OfferInstProxy offerInstProxy = new OfferInstProxy.Builder().httpClient(httpClient).build();
            params.add(offerId);
            params.add(eventMeta.getRegionId());
            serverProxyReq.setParams(params.toArray(new Long[] {}));
            sb.append(offerInstProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        }else {
          sb.append(",");
        }
      } else if (JsonElementName.OFFER_PROD_INST_DETAILS.equals(jsonNodeName)) {
        if (eventMeta.getProdInstId() != null) {
          for (Long prodId : eventMeta.getProdInstId()) {
            ProdInstProxy prodInstProxy = new ProdInstProxy.Builder().httpClient(httpClient).build();
            params.add(prodId);
            params.add(eventMeta.getRegionId());
            serverProxyReq.setParams(params.toArray(new Long[] {}));
            sb.append(prodInstProxy.queryServerMsg(serverProxyReq)).append(",");
          }
        }else {
          sb.append(",");
        }

      } else if (JsonElementName.EVENT_MSG.equals(jsonNodeName)) {
        sb.append(eventMeta.getContent()).append(",");
      }
    } catch (Exception e){
      log.info("query event node {} failed", jsonNodeName,e );
    }


    Pair<String, String> pair = new Pair<String, String>(jsonNodeName,
        sb.toString().substring(0, sb.toString().length() - 1));
    return pair;
  }


}
